﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace P20_2 {
    internal class Program {
        static void Main(string[] args) {
            /**
             * form：事件拥有者
             * this.form.Click：事件
             * con：事件响应者
             * this.FormClicked：事件处理器
             * this.form.Click += this.FormClicked;：事件订阅
             */
            Form form = new Form();
            Controller con = new Controller(form);
            //这里用 form 触发是因为 Controller(form); 的构造方法是引用传递，myform 与 form 的地址值是一样的，
            //所以这里用form触发事件
            form.ShowDialog();
        }
    }
        class Controller {
            private Form myForm;
            public Controller(Form form) {
                if (form != null) {
                this.myForm = form;
                //这里其实是给 form 绑定事件，因为 myForm 与 form 的地址值是一样的
                this.myForm.Click += this.FormClicked;
                Console.WriteLine(myForm.GetHashCode());
                Console.WriteLine(form.GetHashCode());
                }
            }
            //事件处理器
            private void FormClicked(object sender, EventArgs e) {
                this.myForm.Text = DateTime.Now.ToString();
            }
        }
}
